import numpy as np
import pandas as pd
import skfuzzy as fuzz
from skfuzzy import control as ctrl

# Load the dataset
data = pd.read_csv(r'D:\jisuanji\clips\MagicTelescope.csv')
# 创建输入变量
fLength = ctrl.Antecedent(np.arange(0, 101, 1), 'fLength')
fWidth = ctrl.Antecedent(np.arange(0, 101, 1), 'fWidth')

# 创建输出变量
ID = ctrl.Consequent(np.arange(0, 11, 1), 'ID')

# 定义输入变量的隶属函数
fLength['low'] = fuzz.trimf(fLength.universe, [0, 0, 50])
fLength['medium'] = fuzz.trimf(fLength.universe, [0, 50, 100])
fLength['high'] = fuzz.trimf(fLength.universe, [50, 100, 100])

fWidth['low'] = fuzz.trimf(fWidth.universe, [0, 0, 50])
fWidth['medium'] = fuzz.trimf(fWidth.universe, [0, 50, 100])
fWidth['high'] = fuzz.trimf(fWidth.universe, [50, 100, 100])

# 定义输出变量的隶属函数
ID['low'] = fuzz.trimf(ID.universe, [0, 0, 5])
ID['medium'] = fuzz.trimf(ID.universe, [0, 5, 10])
ID['high'] = fuzz.trimf(ID.universe, [5, 10, 10])

# 定义模糊规则
rule1 = ctrl.Rule(fLength['high'] & fWidth['low'], ID['high'])
rule2 = ctrl.Rule(fLength['medium'] & fWidth['medium'], ID['medium'])
rule3 = ctrl.Rule(fLength['low'] & fWidth['high'], ID['low'])

# 创建模糊控制系统
fan_ctrl = ctrl.ControlSystem([rule1, rule2, rule3])
fan = ctrl.ControlSystemSimulation(fan_ctrl)

# 输入数据
fan.input['fLength'] = 75
fan.input['fWidth'] = 30

# 运行模糊控制器
fan.compute()

# 输出结果
print(fan.output['ID'])
